<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="../js/jquery-1.9.1.js"></script>
<script src="../js/jquery.form.min.js"></script>

<script type="text/javascript">
	
	function showRequest(){}
	
	function submitForm(){
		$('#examfr').ajaxSubmit({
			type: 'post',
			url:'/upms/exam/maintain!add.action',			
			beforeSubmit:  showRequest,  
			success: showResponse,
			error: function(XmlHttpRequest, textStatus, errorThrown){
					 alert( "error");  
			}
		}); 
	}	
	
	function showResponse(responseText, statusText, xhr, $form){	   
		$('#exam_shortName').attr("disabled","disabled");
		$('#exam_name').attr("disabled","disabled");
		$('#fisrtButton').hide();
		$('#addButton').show();
		
		var json = eval("(" + responseText + ")");
		if(json.examId){
			$('#hiddenExamId').attr("value",json.examId);
			addQuestion(json.examId);
		}
		
	}
	
	function addQuestion(){
		var examId = $('#hiddenExamId').val(); 
		window.open("/upms/exam/question.action?examId="+examId, "", "height=550,width=1200, top=100, left=150");
	}
	
	function updateQuestion(questionId){
		window.open("/upms/exam/question.action?questionId="+questionId, "", "height=550,width=1200, top=100, left=150");
	}
	
	function deleteQuestion(questionId){
		$("#question_"+questionId).remove();
		$("#maintainQues_"+questionId).remove();  
		
        $.ajax({
    	    type: 'POST',
    		url: "/upms/exam/question!delete.action",
    		data: {questionId: questionId}
    	});
	}
	
	
</script>		
</head>

	<body>
		<h3 align="left">
				<s:if test="%{examId>0}">
					Update Examination
				</s:if>
				<s:else>
					Add New Examination
				</s:else>				
		</h3>
		
		<form method="post" id="examfr">	
			<input type="hidden" name="hiddenExamId" id="hiddenExamId" value="${examId}" />	
		  
		    <div style="position:relative">	
				<table  align="left" border="0">
	
					<tr>
					   <td align="right">Exam Short Name:</td>
						<td>
							<input type="text" name="exam.shortName" value="${exam.shortName}" id="exam_shortName" />
						</td>
						<td>&nbsp;</td>
					</tr>			
					<tr>
					   <td align="right">Exam Name:</td>
						<td>
							<input type="text" name="exam.name" value="${exam.name}" id="exam_name" size="100"/>
						</td>
						<td>&nbsp;</td>
					</tr>					
				</table>
			</div>

		</form>
		
		<div id="questionList">
			<s:iterator value="exam.questions" id="ques" status="status">
				<div style="width:1000px;float:left;padding:5px 5px 5px 5px;border:1px;border-color:#f5f5f5;border-style:solid;" id="question_<s:property value="#ques.questionId" />">    
					<b><s:property value="#status.count" />.</b> <s:property value="#ques.content" />	
							<s:if test="%{#ques.questionType==1}">
								(Single Select)	
							</s:if>
							<s:else>
								(Multiple Select)
							</s:else>					
					<br/>
					<s:iterator value="#ques.choices" id="chos" status="stas">
							&nbsp;&nbsp;&nbsp;&nbsp;<s:property value="#stas.count" />.&nbsp;<s:property value="#chos.choiceContent" />	
						<br />		
					</s:iterator>	
				</div>
				<div style="float:left;position: relative; text-align: center; top: 5px; width:50px;height:50px;" id="maintainQues_<s:property value="#ques.questionId" />">
					<a href="#" onclick="updateQuestion(<s:property value="#ques.questionId" />);">编辑</a><br/><a href="#" onclick="deleteQuestion(<s:property value="#ques.questionId" />);">删除</a>
				</div>
			</s:iterator>
		</div>
		
		<div style="clear:both; display:block"></div>
		
		<s:if test="%{examId>0}">		
			<div style="position:relative;" id="addButton">	
				<table width="1000px"  border="0">
					<tr><td align="right" height=20px>
							<input type=button value="Add Question" onclick="addQuestion();"/>
					</td></tr>
				</table>
			</div>
		</s:if>
		<s:else>
			<div style="position:relative" id="fisrtButton">	
				<table width="1000px"  border="0">
					<tr><td align="right" height=20px>
							<input type=button value="Add Question" onclick="submitForm();"/>
					</td></tr>
				</table>
			</div>
			
			<div style="position:relative;display:none" id="addButton">	
				<table width="1000px"  border="0">
					<tr><td align="right" height=20px>
							<input type=button value="Add Question" onclick="addQuestion();"/>
					</td></tr>
				</table>
			</div>
		</s:else>

						

			
	</body>
</html>